<template>
  <div class="detail">
    <!--导航-->
    <detail-nav-bar class="detail-nav" @titleClick='titleClick' ref="nav"></detail-nav-bar>
    <scroll class="content" ref="scroll" @scroll="contentScroll" :probe-type="3">
      <!--轮播图-->
      <detail-swiper :top-images="topImages"></detail-swiper>
      <!--商品相关信息-->
      <detail-base-info :goods="goods"></detail-base-info>
      <!--商家相关信息-->
      <detail-shop-info :shop="shop"></detail-shop-info>
      <!--商品相关详情 利用watch处理图片频繁加载-->
      <detail-goods-info :detail-info="detailInfo" @imageLoad='imageLoad'></detail-goods-info>
      <!--商品参数详情-->
      <detail-param-info :param-info="paramInfo" ref="params"></detail-param-info>
      <!--商品评论相关-->
      <detail-comment-info :comment-info="commentInfo" ref="comment"></detail-comment-info>
      <!--推荐商品详情 利用minin里面的debounce处理图片加载完毕频繁向外发送事件-->
      <goods-list :goods="recommends" ref="recommend"></goods-list>
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
  </div>
</template>
<script>
  import DetailNavBar from "./childComps/DetailNavBar";
  import DetailSwiper from "./childComps/DetailSwiper";
  import DetailBaseInfo from "./childComps/DetailBaseInfo";
  import DetailShopInfo from "./childComps/DetailShopInfo";
  import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
  import DetailParamInfo from "./childComps/DetailParamInfo";
  import DetailCommentInfo from "./childComps/DetailCommentInfo";
  import DetailBottomBar from "./childComps/DetailBottomBar";

  import Scroll from "components/common/scroll/Scroll";
  import GoodsList from "components/content/goods/GoodsList";


  import { getDetail, Goods, Shop, GoodsParam, getRecommend } from "@/network/detail";
  import { itemListenerMixin, backTopMixin } from "common/mixin";
  import { debounce } from "@/components/common/utils";


  export default {
    name: "Detail",
    mixins: [itemListenerMixin, backTopMixin],
    data() {
      return {
        iid: null,
        // 轮播图展示数据
        topImages: [],
        // 商品展示相关数据
        goods: {},
        // 商家店铺展示相关数据
        shop: {},
        // 商品详细图片数据
        detailInfo: {},
        // 商品参数
        paramInfo: {},
        // 评论信息
        commentInfo: {},
        // 相关推荐数据
        recommends: [],
        // 每一个模块距离顶部的距离集合在数组中
        themeTopYs: [],
        // 在每张图片加载完毕后给themeTopYs赋值利用防抖函数处理的函数返回值,为了提高性能
        getThemeTopY: null,
        currentIndex: 0,
      }
    },
    components: {
      DetailNavBar,
      DetailSwiper,
      DetailBaseInfo,
      DetailShopInfo,
      Scroll,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      GoodsList,
      DetailBottomBar,
    },
    created() {
      // 保存传入的iid
      this.iid = this.$route.params.iid
      // 根据iid请求详情数据
      getDetail(this.iid).then(res => {
        // 获取顶部image轮播图数据
        console.log(res);
        const data = res.result;
        this.topImages = data.itemInfo.topImages
        // 获取商品信息
        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

        // 获取店铺信息的对象
        this.shop = new Shop(data.shopInfo)

        // 保存商品的详情数据
        this.detailInfo = data.detailInfo;

        // 获取参数信息
        this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

        // 取出评论信息
        if (data.rate.cRate !== 0) {
          this.commentInfo = data.rate.list[0]
        }
        // 给getThemeTopY赋值
        this.getThemeTopY = debounce(() => {
          this.themeTopYs = [];
          this.themeTopYs.push(0)
          this.themeTopYs.push(this.$refs.params.$el.offsetTop);
          this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
          this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
          // 额外添加一个非常大的值， 便于后续for田间判断
          this.themeTopYs.push(Number.MAX_VALUE)
          console.log(this.themeTopYs);
        }, 100)
      })
      // 请求推荐数据
      getRecommend().then(res => {
        // console.log(res);
        this.recommends = res.data.list
      })
    },
    methods: {
      // 监听图片加载完毕,重新刷新scroll里面可以滚动的区域
      imageLoad() {
        // 图片加载后刷新可滚动区域的高度
        this.refresh()
        // 利用防抖函数给themeTopYs赋值返回函数的调用,保证只是调用一次
        this.getThemeTopY()
      },
      titleClick(index) {
        console.log(index)
        this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 100)
      },

      contentScroll(position) {
        // console.log(position);
        // 获取Y值
        const positionY = -position.y
        // 和主题中的position做对比

        /*
        * themeTooYs = [a, b, c, d]
        *   如果在positionY值介于 a - b 之间,则index设置为0
        *   如果在positionY值介于 b - c 之间,则index设置为1
        *   如果在positionY值介于 c - d 之间,则index设置为2
        *   如果在positionY > d 之间, 则index设置为3
        *
        * */
        const length = this.themeTopYs.length;
        for (let i = 0; i < length - 1; i++) {
          if (this.currentIndex !== i && (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])) {
            this.currentIndex = i;
            this.$refs.nav.currentIndex = this.currentIndex;
          }
        }
        //   if(positionY > this.themeTopYs[length - 2]) {
        //     this.$refs.nav.currentIndex = length - 2
        //   }else if (positionY >= this.themeTopYs[length - 3]) {
        //     this.$refs.nav.currentIndex = length - 3
        //   }else if (positionY >= this.themeTopYs[length-4]) {
        //     this.$refs.nav.currentIndex = length - 4
        //   }else if(positionY >= this.themeTopYs[length-5]) {
        //     this.$refs.nav.currentIndex = length - 5
        //   }
        // 是否显示回到顶部
        this.isShowBackTop = (-position.y) > 1000
      },
      addToCart() {
        // console.log('----------')
        // 先获取购物车需要展示的信息
        const product = {}
        product.image = this.topImages[0];
        product.title = this.goods.title;
        product.desc = this.goods.desc;
        product.price = this.goods.realPrice;
        product.iid = this.iid;
        // 将商品添加进购物车
        this.$store.dispatch('addCart', product).then(res => {
          // // console.log(res);
          // this.show = true;
          // this.message = res
          //
          // setTimeout(() => {
          //   this.show = false;
          //   this.message = ''
          // } , 1500)
          this.$toast.show(res , 1500);
        })

        // 添加购物车成功

      },
    },
    mounted() {},
    // 取消全局监听事件
    destroyed() {
      console.log('destory')
      this.$bus.$off('itemImgLoad', this.itemImgListener)
    }
  }
</script>
<style scoped>
  .detail {
    position: relative;
    z-index: 999;
    /*100vh表示是视口高度*/
    height: 100vh;
    background-color: #fff;
  }

  .detail-nav {
    position: relative;
    z-index: 999;
    background-color: #fff;
  }

  .content {
    /*屏幕高度 - 顶部navBar的高度 - 底部信息的高度*/
    height: calc(100% - 44px - 58px);
    overflow: hidden;
  }

</style>
